from sqlalchemy.orm import Session
from . import models, schemas

def get_cover_reply(db: Session, cover_reply_id: int):
    return db.query(models.CoverReply).filter(models.CoverReply.id == cover_reply_id).first()

def get_cover_replies(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.CoverReply).offset(skip).limit(limit).all()

def create_cover_reply(db: Session, cover_reply: schemas.CoverReplyCreate):
    db_cover_reply = models.CoverReply(**cover_reply.dict())
    db.add(db_cover_reply)
    db.commit()
    db.refresh(db_cover_reply)
    return db_cover_reply

def update_cover_reply(db: Session, cover_reply_id: int, cover_reply: schemas.CoverReplyUpdate):
    db_cover_reply = db.query(models.CoverReply).filter(models.CoverReply.id == cover_reply_id).first()
    if db_cover_reply:
        for var, value in vars(cover_reply).items():
            setattr(db_cover_reply, var, value) if value else None
        db.add(db_cover_reply)
        db.commit()
        db.refresh(db_cover_reply)
    return db_cover_reply

def delete_cover_reply(db: Session, cover_reply_id: int):
    db_cover_reply = db.query(models.CoverReply).filter(models.CoverReply.id == cover_reply_id).first()
    if db_cover_reply:
        db.delete(db_cover_reply)
        db.commit()
    return db_cover_reply